<?php

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\console\input\Argument;
use app\common\model\User;

class CreateSuperAdmin extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('user:createsuperadmin')
            ->setDescription('添加超级管理员，所有操作忽略权限限制');
        // 设置参数
        $this->addArgument('username', Argument::REQUIRED, '用户名')
            ->addArgument('password', Argument::REQUIRED, '密码')
            ->addArgument('realname', Argument::REQUIRED, '真实姓名');
    }

    protected function execute(Input $input, Output $output)
    {
    	// 指令输出
        $username = $input->getArgument('username');
        $password = $input->getArgument('password');
        $realname = $input->getArgument('realname');

        if (User::where('username', $username)->find()) {
            $output->error('用户已经存在！');
            return;
        }

        $user = new User([
            'username' => $username,
            // 'password' => $password, 密码需要经过hash
            'realname' => $realname,
            'role' => 0,
            'is_superadmin' => true,
            'is_active' => true,
        ]);
        $user->setPassword($password);
        $user->save();

        $output->writeln('超级管理员' . $username . '添加成功');
    }
}
